﻿using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace PMS.SANY.Api
{
    public static class SwaggerExtentions
    {
        /// <summary>
        /// 配置Swashbuckle文档
        /// https://docs.microsoft.com/zh-cn/aspnet/core/tutorials/getting-started-with-swashbuckle
        /// </summary>
        /// <param name="services"></param>
        /// <returns></returns>
        public static IServiceCollection AddSwashbuckle(this IServiceCollection services)
        {
            services.AddSwaggerGen(options =>
            {
                options.SwaggerDoc("v1", new OpenApiInfo()
                {
                    Version = "v1",
                    Title = "ZEQP PMS API",
                    Description = "生产管理系统Api框架 <br />中南智能装备有限公司<br /><br /><a href=\"#operations-tag-Operator\">登录</a>"
                });
                options.AddSecurityDefinition("Token", new OpenApiSecurityScheme()
                {
                    Name = "Authorization",
                    Type = SecuritySchemeType.Http,
                    In = ParameterLocation.Header,
                    Scheme = JwtBearerDefaults.AuthenticationScheme,
                    Description = "JWT Bearer认证"
                });
                options.AddSecurityRequirement(new OpenApiSecurityRequirement()
                {
                    {
                        new OpenApiSecurityScheme
                        {
                            Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "Token" }
                        },
                        new[] { "readAccess", "writeAccess" }
                    }
                });
                var listFile = new List<string>() {
                    "PMS.SANY.Util","PMS.SANY.Entity","PMS.SANY.Models","PMS.SANY.Api"
                };
                var listPath = listFile.Select(s => Path.Combine(AppContext.BaseDirectory, $"{s}.xml"));
                foreach (var file in listPath)
                {
                    if (File.Exists(file))
                        options.IncludeXmlComments(file);
                }
            });
            return services;
        }

        /// <summary>
        /// 配置NSwag文档
        /// https://docs.microsoft.com/zh-cn/aspnet/core/tutorials/getting-started-with-nswag
        /// </summary>
        /// <param name="services"></param>
        /// <returns></returns>
        public static IServiceCollection AddNSwag(this IServiceCollection services)
        {
            //services.AddSwaggerDocument(options =>
            //{
            //    options.AllowReferencesWithProperties = true;
            //    options.AddSecurity("Token", Enumerable.Empty<string>(), new OpenApiSecurityScheme()
            //    {
            //        Scheme = JwtBearerDefaults.AuthenticationScheme,
            //        Description = "JWT Bearer认证",
            //        In = OpenApiSecurityApiKeyLocation.Header,
            //        Type = OpenApiSecuritySchemeType.Http
            //    });
            //});
            return services;


            ////OpenApi
            //app.UseOpenApi();

            ////API文档页面
            //app.UseSwaggerUi3();
        }
    }

}
